%% corn
[ri,oi] = bkfunc_genfutrollinfo('corn');
[cfut,crt,ci] = bkfunc_buildcontinuousfutures(ri,oi);
[res] = bkfunc_hvcalib(crt,'forecastperiod',21,'printresults',true,...
    'plotconditonalvariance',false,'scalefactor',sqrt(252))
%%
code = ri{end,5};hd = cDataFileIO.loadDataFromTxtFile([code,'_daily.txt']);
kdiff = 20;ck = floor(hd(end,5)/kdiff)*kdiff;
% k = [ck-3*kdiff;ck-2*kdiff;ck-kdiff;ck;ck+kdiff;ck+2*kdiff;ck+3*kdiff];
k = [ck-2*kdiff;ck-kdiff;ck;ck+kdiff;ck+2*kdiff];
c = cell(length(k),1);p=c;pnlc=c;pnlp=p;lbd=getlastbusinessdate;
for i = 1:length(k)
    c{i} = [code,'-C-',num2str(k(i))];
    p{i} = [code,'-P-',num2str(k(i))];
    pnlc{i} = pnlriskbreakdown1(c{i},lbd);
    pnlp{i} = pnlriskbreakdown1(p{i},lbd);
end
%%
fprintf('\n%s:%s\n',datestr(lbd,'yyyy-mm-dd'),code);
fprintf('calls pnl breakdown:\n');
fprintf('%9s%9s%9s%9s%9s%9s%9s%10s%10s%9s%9s\n',...
    'strike','total','theta','delta','gamma','vega','other','ivlast','div','slast','ds');
for i = 1:length(k)
    fprintf('%9s%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%%%9.1f%%%9s%9s\n',...
        num2str(k(i)),...
        pnlc{i}.pnltotal,...
        pnlc{i}.pnltheta,...
        pnlc{i}.pnldelta,...
        pnlc{i}.pnlgamma,...
        pnlc{i}.pnlvega,...
        pnlc{i}.pnlunexplained,...
        pnlc{i}.iv2*100,...
        (pnlc{i}.iv2-pnlc{i}.iv1)*100,...
        num2str(pnlc{i}.spot2),...
        num2str(pnlc{i}.spot2-pnlc{i}.spot1));
end
fprintf('\n');
fprintf('puts pnl breakdown:\n');
fprintf('%9s%9s%9s%9s%9s%9s%9s%10s%10s%9s%9s\n',...
    'strike','total','theta','delta','gamma','vega','other','ivlast','div','slast','ds');
for i = 1:length(k)
    fprintf('%9s%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%%%9.1f%%%9s%9s\n',...
        num2str(k(i)),...
        pnlp{i}.pnltotal,...
        pnlp{i}.pnltheta,...
        pnlp{i}.pnldelta,...
        pnlp{i}.pnlgamma,...
        pnlp{i}.pnlvega,...
        pnlp{i}.pnlunexplained,...
        pnlp{i}.iv2*100,...
        (pnlp{i}.iv2-pnlp{i}.iv1)*100,...
        num2str(pnlp{i}.spot2),...
        num2str(pnlp{i}.spot2-pnlp{i}.spot1));
end
